//
//  KDReginDataTool.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/8/11.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDReginDataTool.h"
#import "FMDB.h"
#import "KDReginType.h"
#import "KDGetAllReginDataResult.h"
#import "KDGetAllReginDataParam.h"
#import "KDPatrolPageRequestTool.h"

@implementation KDReginDataTool
static FMDatabase *_db;
- (NSString *)readDatabaseWithSqliteName:(NSString *)dbname{
    bool success;
    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString * writableDBPath = [documentsDirectory stringByAppendingPathComponent:dbname];
    success = [manager fileExistsAtPath:writableDBPath];
    if (!success) {
        NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
        success = [manager copyItemAtPath:defaultPath toPath:writableDBPath error:&error];
        if (!success) {
            KDLog(@"Faield to create table file with reginData!'%@'.",[error localizedDescription]);
        }
    }
    return writableDBPath;
}
+ (void)initialize
{
    //1.打开数据库
    NSString *path = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"db"];
    KDLog(@"%@", path);
    _db = [FMDatabase databaseWithPath:path];
    [_db open];
    //2.创表
//    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS com_kedun_yqpt_report_address_AddressBean (id integer PRIMARY KEY AUTOINCREMENT, parentId INTEGER,level INTEGER,name TEXT);"];
   
}
/**
 *  插入所有数据
 */
+ (void)insertDataToTable{
    KDGetAllReginDataParam *param = nil;
    [KDPatrolPageRequestTool getAllReginDataWithParam:param success:^(KDGetAllReginDataResult *responseObejct) {
        for (KDReginType *model in responseObejct.data) {
            [_db executeUpdateWithFormat:@"INSERT INTO com_kedun_yqpt_report_address_AddressBean(id,parentId,level,name) VALUES(%ld,%ld,%ld,%@);", (long)model.ID,(long)model.parentId, (long)model.level, model.name];
        }
    } failure:^(NSError *error) {
        KDLog(@"%@", error);
    }];
}

/**
 *  根据parentId查找对应的数组模型
 */
+ (NSArray *)reginTypeArrWithParentId:(NSInteger)parentId{
    NSString *sql = [NSString stringWithFormat:@"SELECT *FROM com_kedun_yqpt_report_address_AddressBean WHERE parentId = %ld", (long)parentId];
    FMResultSet *set = [_db executeQuery:sql];
    NSMutableArray *temps = [NSMutableArray array];
    while (set.next) {
        //NSData ————》NSDictionary
        int ID = [[set objectForColumnName:@"id"] intValue];
        int parentID = [[set objectForColumnName:@"parentId"]intValue];
        int level = [[set objectForColumnName:@"level"] intValue];
        NSString *name = [set objectForColumnName:@"name"];
        KDReginType *model = [[KDReginType alloc]init];
        model.ID = ID;
        model.parentId = parentID;
        model.level = level;
        model.name = name;
        [temps addObject:model];
    }
    NSArray *reginTypes = [NSArray arrayWithArray:temps];
    if (reginTypes.count == 0) {
        return nil;
    }
    return reginTypes;
}
/**
 *   根据parentId查找对应的数组模型(微博表)
 *
 *  @param parentId 所属父ID
 *
 *  @return 该父id旗下的所有子id
 */
+(NSArray *)weiboReginTypeArrWithParentId:(NSInteger)parentId{
    NSString *sql = [NSString stringWithFormat:@"SELECT *FROM address_sina_weibo WHERE parentId = %ld", (long)parentId];
    FMResultSet *set = [_db executeQuery:sql];
    NSMutableArray *temps = [NSMutableArray array];
    while (set.next) {
        //NSData ————》NSDictionary
        int ID = [[set objectForColumnName:@"id"] intValue];
        int parentID = [[set objectForColumnName:@"parentId"]intValue];
        int level = [[set objectForColumnName:@"level"] intValue];
        NSString *name = [set objectForColumnName:@"name"];
        KDReginType *model = [[KDReginType alloc]init];
        model.ID = ID;
        model.parentId = parentID;
        model.level = level;
        model.name = name;
        [temps addObject:model];
    }
    NSArray *reginTypes = [NSArray arrayWithArray:temps];
    if (reginTypes.count == 0) {
        return nil;
    }
    return reginTypes;
}
/**
 *  根据现有的areaID获取到对应所属的行政区域
 *
 *  @param ID 传入你的地域ID
 *
 *  @return 返回给你该ID的地域名称
 */
+ (NSString *)getAreaNameWithID:(NSString *)ID{
    KDReginType *model = [[KDReginType alloc]init];
    NSMutableString *areaName = [NSMutableString string];
    model.ID = ID.intValue;
    if (model.ID == 0) {
        areaName = [NSMutableString stringWithString:@"全国"];
    }else{
        while (model.ID != 0) {
            NSString *sql1 = [NSString stringWithFormat:@"SELECT *FROM com_kedun_yqpt_report_address_AddressBean WHERE id = %ld", (long)model.ID];
            FMResultSet *set = [_db executeQuery:sql1];
            while (set.next) {
                //NSData ————》NSDictionary
                int parentID = [[set objectForColumnName:@"parentId"]intValue];
                NSString *name = [NSString stringWithFormat:@"%@  ", [set objectForColumnName:@"name"]];
                //赶紧换成父ID
                model.ID = parentID;
                model.name = name;
            }
            [areaName insertString:model.name atIndex:0];
        }
        [areaName deleteCharactersInRange:NSMakeRange(areaName.length - 2, 2)];
    }
    return areaName;
}

@end
